package com.efast.dmom.mgmt.service.mes.order.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.efast.cafe.framework.bean.ResultBean;
import com.efast.cafe.framework.redis.ObjectRedisTemplate;
import com.efast.dmom.mgmt.bean.mes.order.RecycledVinBean;
import com.efast.dmom.mgmt.dao.mes.order.VinRecycleDao;
import com.efast.dmom.mgmt.service.mes.order.VinRecycleService;
import com.efast.dmom.mgmt.utils.MesConstant;

@Service
public class VinRecycleServiceImpl implements VinRecycleService {

	@Autowired
	private ObjectRedisTemplate objectRedisTemplate;
	
	@Autowired
	private VinRecycleDao recycleDao;

	private final String RECYCLED_VIN_LIST = "RECYCLED_VIN_LIST";

	public List<RecycledVinBean> refreshCache() {

		List<RecycledVinBean> vinList = new ArrayList<RecycledVinBean>();

		boolean hasKey = objectRedisTemplate.hasKey(RECYCLED_VIN_LIST);
		if (!hasKey) {
			objectRedisTemplate.opsForValue().set(RECYCLED_VIN_LIST, vinList);
		}

		vinList = recycleDao.getAllRecycledVinByStatus(MesConstant.RECYCLED);
		objectRedisTemplate.opsForValue().set(RECYCLED_VIN_LIST, vinList);

		return vinList;
	}

	@SuppressWarnings("unchecked")
	@Override
	public void recycleVin(List<RecycledVinBean> objList) {
        
			List<RecycledVinBean> cachedBeanList = (List<RecycledVinBean>) objectRedisTemplate.opsForValue()
					.get(RECYCLED_VIN_LIST);

			if (cachedBeanList == null || cachedBeanList.size() <= 0) {
				cachedBeanList = refreshCache();
			}
			if (objList != null && objList.size() > 0) {
			for(RecycledVinBean bean:objList) {
			    cachedBeanList.add(bean);
			}
			objectRedisTemplate.opsForValue().set(RECYCLED_VIN_LIST, cachedBeanList);
			}
	}

	@SuppressWarnings("unchecked")
	@Override
	public ResultBean fetchRecycledVin() {

		ResultBean resultBean = new ResultBean(true, null);

		List<RecycledVinBean> vinListBean = (List<RecycledVinBean>) objectRedisTemplate.opsForValue()
				.get(RECYCLED_VIN_LIST);

		if (vinListBean == null || vinListBean.size() <= 0) {
			vinListBean = refreshCache();
		}

		resultBean.setObject(vinListBean);

		objectRedisTemplate.opsForValue().set(RECYCLED_VIN_LIST, vinListBean);
		return resultBean;
	}

	@Override
	public void updateRecyclePool(List<RecycledVinBean> vinListBean) {

		objectRedisTemplate.opsForValue().set(RECYCLED_VIN_LIST, vinListBean);
	}

	@SuppressWarnings("unchecked")
	@Override
	public void removeRecycledBean(RecycledVinBean recycledBean) {

		if (recycledBean != null) {

			List<RecycledVinBean> vinBeanList = (List<RecycledVinBean>) objectRedisTemplate.opsForValue()
					.get(RECYCLED_VIN_LIST);
			
			if(vinBeanList == null){
				vinBeanList = refreshCache();
			}

			int removedIndex = -1;
			
			if (vinBeanList != null && vinBeanList.size() > 0) {

				for (int i=0; i<vinBeanList.size(); i++) {
					
					String vin = vinBeanList.get(i).getVin();

					if (vin != null && vin.equals(recycledBean.getVin())) {

						removedIndex = i;
						break;
					}
				}
			}
			
			if(removedIndex >= 0){
				vinBeanList.remove(removedIndex);
			}
			
			if(vinBeanList != null){
				objectRedisTemplate.opsForValue().set(RECYCLED_VIN_LIST, vinBeanList);
			}
			
		}
	}

	@Override
	public void updateRecycleBeanStatus(List<RecycledVinBean> vinList) {

		recycleDao.updateRecycleBeanStatus(vinList);
	}

}